package com.com.Demo;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class TCPClient {
    public static void main(String[] args) throws Exception {
// 1. 调用Socket()创建一个流套接字，并请求连接到服务器端。
// Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8080);
        Socket socket = new Socket("192.168.137.1", 8089);
// 2. 调用Socket类的getOutputStream和getInputStream获取输出流和输入流，开始网络
// 获取输出流：往服务器端写数据
        OutputStream os = socket.getOutputStream();
        String data = "你好";
        os.write(data.getBytes(), 0, data.getBytes().length);
// 获取服务器的响应流
        InputStream is = socket.getInputStream();
        //BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
        InputStreamReader inputStreamReader = new InputStreamReader(is);


        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = is.read(buffer)) > 0) {
            String str = new String(buffer, 0, len);
            System.out.println("客户端接收服务器的响应数据：" + str);
        }
// 3. 最后关闭通信套接字。
        os.close();
        socket.close();
    }
}
